package cn.iocoder.yudao.module.erp.dal.mysql.dispatchvehicle;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.erp.dal.dataobject.dispatchvehicle.DispatchVehicleDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 总配车信息 Mapper
 * @author 天使管理员
 */
@Mapper
public interface DispatchVehicleMapper extends BaseMapperX<DispatchVehicleDO> {

    default PageResult<DispatchVehicleDO> selectPage(DispatchVehiclePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DispatchVehicleDO>()
                .eqIfPresent(DispatchVehicleDO::getDispatchNumber, reqVO.getDispatchNumber())
                .eqIfPresent(DispatchVehicleDO::getIsSplit, reqVO.getIsSplit())
                .betweenIfPresent(DispatchVehicleDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(DispatchVehicleDO::getTransportationStatus, reqVO.getTransportationStatus())
                .eqIfPresent(DispatchVehicleDO::getFare, reqVO.getFare())
                .eqIfPresent(DispatchVehicleDO::getDpId, reqVO.getDpId())
                .eqIfPresent(DispatchVehicleDO::getNo, reqVO.getNo())
                .eqIfPresent(DispatchVehicleDO::getOrigin, reqVO.getOrigin())
                .eqIfPresent(DispatchVehicleDO::getDest, reqVO.getDest())
                .eqIfPresent(DispatchVehicleDO::getLineDetails, reqVO.getLineDetails())
                .eqIfPresent(DispatchVehicleDO::getSupplier, reqVO.getSupplier())
                .eqIfPresent(DispatchVehicleDO::getInventoryStatus, reqVO.getInventoryStatus())
                .eqIfPresent(DispatchVehicleDO::getPackagesTotal, reqVO.getPackagesTotal())
                .eqIfPresent(DispatchVehicleDO::getWeightTotal, reqVO.getWeightTotal())
                .eqIfPresent(DispatchVehicleDO::getVolumeTotal, reqVO.getVolumeTotal())
                .eqIfPresent(DispatchVehicleDO::getDriverPhone, reqVO.getDriverPhone())
                .eqIfPresent(DispatchVehicleDO::getNote, reqVO.getNote())
                .orderByDesc(DispatchVehicleDO::getId));
    }

    default PageResult<DispatchVehicleDO> selectOutPage(DispatchVehiclePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DispatchVehicleDO>()
                .eqIfPresent(DispatchVehicleDO::getDispatchNumber, reqVO.getDispatchNumber())
                .eqIfPresent(DispatchVehicleDO::getTransportationStatus, reqVO.getTransportationStatus())
                .eqIfPresent(DispatchVehicleDO::getNo, reqVO.getNo())
                .eqIfPresent(DispatchVehicleDO::getOrigin, reqVO.getOrigin())
                .eqIfPresent(DispatchVehicleDO::getDest, reqVO.getDest())
                .eqIfPresent(DispatchVehicleDO::getInventoryStatus, reqVO.getInventoryStatus())
                .likeIfPresent(DispatchVehicleDO::getDriverPhone, reqVO.getDriverPhone())
                .orderByDesc(DispatchVehicleDO::getId));
    }

    List<DispatchVehicleDO> getTransportPage(@Param("dpId") String dpId,
                                             @Param("dispatchNumber") String dispatchNumber,
                                             @Param("origin") Integer origin,
                                             @Param("dest") Integer dest,
                                             @Param("driverPhone") String driverPhone,
                                             @Param("pageNo") Integer pageNo,
                                             @Param("pageSize") Integer pageSize);


    Long getTransportPageCount(@Param("dpId") String dpId,
                               @Param("dispatchNumber") String dispatchNumber,
                               @Param("origin") Integer origin,
                               @Param("dest") Integer dest,
                               @Param("driverPhone") String driverPhone);


    default List<DispatchVehicleDO> selectByDispatch(String dispatchNumber, Integer line, Integer inventoryStatus, Integer dest, String dpId){
        return selectList(new LambdaQueryWrapperX<DispatchVehicleDO>()
                .eqIfPresent(DispatchVehicleDO::getDispatchNumber, dispatchNumber)
                .eqIfPresent(DispatchVehicleDO::getLine, line)
                .eqIfPresent(DispatchVehicleDO::getInventoryStatus, inventoryStatus)
                .eqIfPresent(DispatchVehicleDO::getDest, dest)
                .eqIfPresent(DispatchVehicleDO::getDpId, dpId)
                .orderByDesc(DispatchVehicleDO::getId));
    }

    default List<DispatchVehicleDO> selectByDispatchByIds(Integer line, List<String> dpIds){
        return selectList(new LambdaQueryWrapperX<DispatchVehicleDO>()
                .eqIfPresent(DispatchVehicleDO::getLine, line)
                .inIfPresent(DispatchVehicleDO::getDpId, dpIds)
                .orderByDesc(DispatchVehicleDO::getId));
    }

    default List<DispatchVehicleDO> selectByDispatchNumber(String dispatchNumber) {
        return selectList(new LambdaQueryWrapper<DispatchVehicleDO>()
                .eq(DispatchVehicleDO::getDispatchNumber, dispatchNumber)  // 使用eq进行精确匹配
                .orderByDesc(DispatchVehicleDO::getId));  // 按照ID降序排序
    }

    default List<DispatchVehicleDO> selectListByDpId(String dpId) {
        return selectList(new LambdaQueryWrapper<DispatchVehicleDO>()
                .eq(DispatchVehicleDO::getDpId, dpId)
                .orderByDesc(DispatchVehicleDO::getId));
    }


    @Delete("DELETE FROM erp_dispatch_vehicle WHERE dp_id = #{id} AND inventory_status = #{inventoryStatus}")
    Integer deleteDispById(@Param("id") String id, @Param("inventoryStatus") Integer inventoryStatus);

    @Select("SELECT SUM(a.weight_total) from (select dispatch_number,weight_total from erp_dispatch_vehicle where dest in(8,9,10) and inventory_status=2 and update_time BETWEEN #{startTime} AND #{endTime} GROUP BY 1,2 ) a")
    Double getTotalWeightByTimeRange(@Param("startTime") String startTime, @Param("endTime") String endTime);

}